package com.study.happystudy.service.impl;

import com.study.happystudy.mapper.QuestionMapper;
import com.study.happystudy.po.Question;
import com.study.happystudy.service.QuestionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Resource
    QuestionMapper questionMapper;

    public QuestionServiceImpl(QuestionMapper questionMapper) {
        this.questionMapper = questionMapper;
//        System.out.println("QuestionMapper injected: " + questionMapper);
    }

    @Override
    public int addQuestion(String questionType, int score, String description, String options, String correctAnswer, int subjectId, String analysis) {
        return questionMapper.addQuestion(questionType, score, description, options, correctAnswer, subjectId, analysis);
    }

    @Override
    public Question getQuestionById(Integer questionId) {
        System.out.println(questionId);
        return questionMapper.getQuestionById(questionId);
    }

    @Override
    public List<Question> getQuestionsBySubjectId(int subjectId) {
        return questionMapper.getQuestionsBySubjectId(subjectId);
    }

    @Override
    public int deleteQuestionById(int questionId) {
        return questionMapper.deleteQuestionById(questionId);
    }

    @Override
    public List<Question> getQuestionsByType(String questionType) {
        System.out.println("questionType: " + questionType);
//        System.out.println(questionMapper.getQuestionsByType(questionType));
        return questionMapper.getQuestionsByType(questionType);
    }

    @Override
    public List<Question> getQuestionsBySubjectIdAndType(int subjectId, String questionType) {
        return questionMapper.getQuestionsBySubjectIdAndType(subjectId, questionType);
    }


    public List<Question> psychologicalTest(Integer count) {
        // 获取所有心理
        List<Question> questions = questionMapper.getQuestionsBySubjectId(7);

        // count不合法
        if (questions == null || questions.size() < count || count <= 0) {
            throw new IllegalArgumentException("我操你妈");
        }

        // 打乱题目顺序
        List<Question> shuffledQuestions = new ArrayList<>(questions);
        Collections.shuffle(shuffledQuestions);

        // 截取前 count 个元素
        return shuffledQuestions.subList(0, count);
    }


}
